package com.jqb.sturts2lab02.action;

import com.jqb.struts2lab02.bean.Member;
import com.jqb.struts2lab02.util.StringTool;
import com.opensymphony.xwork2.ActionSupport;

public class RegisterValidatorAction extends ActionSupport {

	private static final long serialVersionUID = 1L;

	private Member member;

	public Member getMember() {
		return member;
	}

	public void setMember(Member member) {
		this.member = member;
	}

	@Override
	public String execute() throws Exception {

		return SUCCESS;
	}

	@Override
	public void validate() {
		// this.getText("xxx")获取资源文件信息
		if (StringTool.isEmptyOrNull(member.getUsername())) {
			this.addActionError(this.getText("usernamerequired"));
			this.addFieldError("member.username", "username error");
		}

		if (StringTool.isEmptyOrNull(member.getPassword())) {
			this.addActionError(this.getText("passwordrequired"));
		}

		if (StringTool.isEmptyOrNull(member.getRepassword())) {
			this.addActionError(this.getText("passwordlengtherrorv", new String[]{"4", "6"}));
		}

		if (!member.getPassword().equals(member.getRepassword())) {
			this.addActionError(this.getText("passwordlengtherrorv", new String[]{"4", "6"}));
		}
		if(member.getPassword().length()<4||member.getPassword().length()>6){
			this.addActionError(this.getText("passwordlengtherrorv", new String[]{"4", "6"}));
		}

		if (member.getAge() <= 0 || member.getAge() > 120) {
			this.addActionError(this.getText("ageerror"));
		}

		if (StringTool.isEmptyOrNull(member.getBirthday())) {
			this.addActionError(this.getText("birthdayerror"));
		}

		if (StringTool.isEmptyOrNull(member.getGraduation())) {
			this.addActionError(this.getText("graduationerror"));
		}

	}
}
